package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTabbedPane;
import java.awt.GridBagLayout;
import java.io.File;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.util.HashMap;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JRootPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import code.AsymmetricEncDec;
import code.KeyStoreManager;
import code.Language;
import code.Settings;
import javax.swing.JRadioButton;

public class AsymmetricRSAEncryption extends JPanel {

	Color c = new Color(0x80, 0x80, 0xFF);

	Color TAc = new Color(246, 250, 250);

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;
	private JTabbedPane jTabbedPane = null;
	
	ATOCOmain atocoframe;
	private HashMap labels;
	public int selected;

	private JLabel ajLabel = null;
	private JPanel ajPanel = null;
	private JButton ajButton = null;
	private JButton ajButton2 = null;
	private JLabel ajLabel2 = null;
	private JTextArea ajTextArea = null;
	ButtonGroup arbgroup = new ButtonGroup(); // @jve:decl-index=0:
	ButtonGroup arb2group = new ButtonGroup();
	private JRadioButton ajRadioButton = null;
	private JRadioButton ajRadioButton1 = null;
	private JLabel ajLabel1 = null;
	private JLabel ajLabel3 = null;
	private JLabel ajLabel4 = null;
	private JLabel ajLabel7 = null;
	private JRadioButton ajRadioButton2 = null;
	private JRadioButton ajRadioButton3 = null;
	
	private JLabel bjLabel = null;
	private JPanel bjPanel = null;
	private JButton bjButton1 = null;
	private JButton bjButton2 = null;
	private JLabel bjLabel2 = null;
	private JTextArea bjTextArea = null;
	ButtonGroup brbgroup = new ButtonGroup(); // @jve:decl-index=0:
	ButtonGroup brb2group = new ButtonGroup();
//	private JRadioButton bjRadioButton = null;
	private JRadioButton bjRadioButton1 = null;
	private JLabel bjLabel1 = null;
	private JLabel bjLabel3 = null;
	private JLabel bjLabel4 = null;
	private JLabel bjLabel7 = null;
	private JRadioButton bjRadioButton2 = null;
	private JRadioButton bjRadioButton3 = null;

	private KeyStoreManager ksm;
	private String dataFile;
	String nextclass;
	private PrivateKey privateKey;
	private Certificate cert;
	private Certificate publicKeyCert;
	String padding;
	private Settings s;

	// JScrollPane scroller;

	/**
	 * This is the default constructor
	 */
	public AsymmetricRSAEncryption(ATOCOmain atocoframe) {
		super();
		s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"),
				"AsymmetricRSAEncryption",
				(String) s.global.get("installPath"));
		initialize();

		this.atocoframe = atocoframe;

		this.setPreferredSize(new Dimension(400, 600));

		// Every Keystore operation must use this one.
		ksm = new KeyStoreManager((String) Settings.global
				.get("personalKeyStoreFile"), (String) Settings.global
				.get("personalKeyStorePassword"));
		if (ksm.getKeyStore() == null) {
			System.out.println("Keystore file not found. Creating one.");
			ksm.createKeyStore((String) Settings.global
					.get("personalKeyStoreFile"), (String) Settings.global
					.get("personalKeyStorePassword"));
		}

	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(760, 449);
		this.setLayout(null);
		this.add(getJTabbedPane(), null);
		this.setBackground(Color.WHITE);
	}

	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.setBounds(new Rectangle(35, 70, 745, 400));
			jTabbedPane.addTab("Encyrpt", null, getJPanel(), null);
			jTabbedPane.addTab("Decrypt", null, getJPanel2(), null);
			jTabbedPane.setSelectedIndex(selected);
			jTabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
                public void stateChanged(javax.swing.event.ChangeEvent e) {
                       selected=jTabbedPane.getSelectedIndex();
                }
			});
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		// Encryption mode
		if (ajPanel == null) {
			ajLabel2 = new JLabel();
			ajLabel2.setText((String) labels.get("jLabel2"));
			ajLabel2.setBounds(new Rectangle(481, 15, 71, 17));
			ajLabel2.setForeground(c);
			ajLabel2.setFont(new java.awt.Font("Arial", Font.BOLD, 10));
			ajLabel = new JLabel();
			ajLabel.setBounds(new Rectangle(45, 16, 281, 27));
			ajLabel.setText((String) labels.get("jLabel"));
			ajLabel4 = new JLabel();
			ajLabel4.setBounds(new Rectangle(50, 290, 150, 20));
			ajLabel4.setText((String) labels.get("jLabel4"));
			ajLabel4.setFont(new Font("Arial", Font.ITALIC, 12));
			ajLabel3 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
			ajLabel3.setBounds(new Rectangle(110, 127, 25, 25));
			ajLabel1 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
			ajLabel1.setBounds(new Rectangle(110, 97, 25, 25));
			ajLabel7 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/nextmiddle.gif"));
			ajLabel7.setBounds(new Rectangle(320, 95, 50, 50));
			ajLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (nextclass != null) {
						if (nextclass.equalsIgnoreCase("newkeygui")) {
							atocoframe
									.contentPane(new KeyCreationAsymmetric(
											atocoframe, passclass(),
											"AsymmetricRSAEnc"));
						} else if (nextclass.equalsIgnoreCase("selectkeygui")) {
							atocoframe
									.contentPane(new AsymmetricKeyList(
											atocoframe, passclass(),
											"AsymmetricRSAEnc", "enc"));
						}
					}

				}
			});

			ajPanel = new JPanel();
			ajPanel.setLayout(null);
			ajPanel.add(getaJButton(), null);
			ajPanel.add(getaJButton2(), null);
			ajPanel.setBackground(Color.WHITE);
			ajPanel.add(ajLabel2, null);
			ajPanel.add(getaJTextArea(), null);
			ajPanel.add(getaJRadioButton(), null);
			ajPanel.add(getaJRadioButton1(), null);
			ajPanel.add(ajLabel, null);
			ajPanel.add(ajLabel1, null);
			ajPanel.add(ajLabel3, null);
			ajPanel.add(ajLabel4, null);
			ajPanel.add(ajLabel7, null);
			ajPanel.add(getaJRadioButton2(), null);
			ajPanel.add(getaJRadioButton3(), null);
		}
		return ajPanel;
	}

	private JPanel getJPanel2() {
		// Decryption mode
		if (bjPanel == null) {
			bjLabel2 = new JLabel();
			bjLabel2.setText((String) labels.get("jLabel2"));
			bjLabel2.setBounds(new Rectangle(481, 15, 71, 17));
			bjLabel2.setForeground(c);
			bjLabel2.setFont(new java.awt.Font("Arial", Font.BOLD, 10));
			bjLabel = new JLabel();
			bjLabel.setBounds(new Rectangle(45, 16, 281, 27));
			bjLabel.setText((String) labels.get("jLabel"));
			bjLabel4 = new JLabel();
			bjLabel4.setBounds(new Rectangle(50, 290, 150, 20));
			bjLabel4.setText((String) labels.get("jLabel4"));
			bjLabel4.setFont(new Font("Arial", Font.ITALIC, 12));
			bjLabel3 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
			bjLabel3.setBounds(new Rectangle(110, 127, 25, 25));
//			bjLabel1 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
//			bjLabel1.setBounds(new Rectangle(110, 97, 25, 25));
			bjLabel7 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/nextmiddle.gif"));
			bjLabel7.setBounds(new Rectangle(320, 95, 50, 50));
			bjLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (nextclass != null) {
						if (nextclass.equalsIgnoreCase("newkeygui")) {
							atocoframe
									.contentPane(new KeyCreationAsymmetric(
											atocoframe, passclass(),
											"AsymmetricRSAEnc"));
						} else if (nextclass.equalsIgnoreCase("selectkeygui")) {
							atocoframe
									.contentPane(new AsymmetricKeyList(
											atocoframe, passclass(),
											"AsymmetricRSAEnc", "dec"));
						}
					}

				}
			});

			bjPanel = new JPanel();
			bjPanel.setLayout(null);
			bjPanel.add(getbJButton1(), null);
			bjPanel.add(getbJButton2(), null);
			bjPanel.setBackground(Color.WHITE);
			bjPanel.add(bjLabel2, null);
			bjPanel.add(getbJTextArea(), null);
//			bjPanel.add(getbJRadioButton(), null);
			bjPanel.add(getbJRadioButton1(), null);
			bjPanel.add(bjLabel, null);
//			bjPanel.add(bjLabel1, null);
			bjPanel.add(bjLabel3, null);
			bjPanel.add(bjLabel4, null);
			bjPanel.add(bjLabel7, null);
			bjPanel.add(getbJRadioButton2(), null);
			bjPanel.add(getbJRadioButton3(), null);
		}
		return bjPanel;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getaJButton() {
		if (ajButton == null) {
			ajButton = new JButton();
			ajButton.setBounds(new Rectangle(87, 217, 110, 31));
			ajButton.setText((String) labels.get("jButton"));
			ajButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (!publicKeyCert.equals(null) || !dataFile.equals(null)) {
						new AsymmetricEncDec().encrypt(publicKeyCert.getPublicKey(),
								padding, dataFile);
					}
				}
			});
		}
		return ajButton;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getbJButton1() {
		if (bjButton1 == null) {
			bjButton1 = new JButton();
			bjButton1.setBounds(new Rectangle(210, 217, 110, 31));
			bjButton1.setText((String) labels.get("jButton1"));
			bjButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if ((cert!=null) && (privateKey!=null) && (dataFile!=null)) {
						new AsymmetricEncDec().decrypt(privateKey,
								padding, dataFile);
					}
				}
			});
		}
		return bjButton1;
	}

	/**
	 * This method initializes jButton2
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getaJButton2() {
		if (ajButton2 == null) {
			ajButton2 = new JButton((String) labels.get("jButton2"),
					new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/klasor.gif"));
			ajButton2.setBounds(new Rectangle(136, 54, 146, 37));
			ajButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					browse();
				}
			});
		}
		return ajButton2;
	}
	
	private JButton getbJButton2() {
		if (bjButton2 == null) {
			bjButton2 = new JButton((String) labels.get("jButton2"),
					new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/klasor.gif"));
			bjButton2.setBounds(new Rectangle(136, 54, 146, 37));
			bjButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					browse();
				}
			});
		}
		return bjButton2;
	}

	public void browse() {
		JFileChooser fc = new JFileChooser();
		fc.setDialogTitle((String) labels.get("fc"));
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fc.setMultiSelectionEnabled(true);
		fc.showOpenDialog(this);
		try {
			// Open an input stream
			File f = fc.getSelectedFile();
			// File[] f =fc.getSelectedFiles();
			// System.out.println(f);
			if (!f.equals("null")) {
				String s = f.getPath();
				setDataFile(s);
			}
		} catch (Exception e) {
		}
	}

	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getaJTextArea() {
		if (ajTextArea == null) {
			ajTextArea = new JTextArea();
			ajTextArea.setBounds(new Rectangle(382, 41, 278, 217));
			ajTextArea.setBackground(TAc);
			ajTextArea.setLineWrap(true);
		}
		return ajTextArea;
	}
	
	private JTextArea getbJTextArea() {
		if (bjTextArea == null) {
			bjTextArea = new JTextArea();
			bjTextArea.setBounds(new Rectangle(382, 41, 278, 217));
			bjTextArea.setBackground(TAc);
			bjTextArea.setLineWrap(true);
		}
		return bjTextArea;
	}

	/**
	 * This method initializes jRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getaJRadioButton() {
		if (ajRadioButton == null) {
			ajRadioButton = new JRadioButton();
			ajRadioButton.setBounds(new Rectangle(137, 100, 164, 21));
			ajRadioButton.setText((String) labels.get("jRadioButton"));
			ajRadioButton.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if (ajRadioButton.isSelected()) {
						nextclass = "newkeygui";
					}

				}
			});
			arbgroup.add(ajRadioButton);
		}
		return ajRadioButton;
	}
	
//	private JRadioButton getbJRadioButton() {
//		if (bjRadioButton == null) {
//			bjRadioButton = new JRadioButton();
//			bjRadioButton.setBounds(new Rectangle(137, 100, 164, 21));
//			bjRadioButton.setText((String) labels.get("jRadioButton"));
//			bjRadioButton.addItemListener(new java.awt.event.ItemListener() {
//				public void itemStateChanged(java.awt.event.ItemEvent e) {
//					if (bjRadioButton.isSelected()) {
//						nextclass = "newkeygui";
//					}
//
//				}
//			});
//			brbgroup.add(bjRadioButton);
//		}
//		return bjRadioButton;
//	}

	/**
	 * This method initializes jRadioButton1
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getaJRadioButton1() {
		if (ajRadioButton1 == null) {
			ajRadioButton1 = new JRadioButton();
			ajRadioButton1.setBounds(new Rectangle(137, 130, 165, 21));
			ajRadioButton1.setText((String) labels.get("jRadioButton1"));
			ajRadioButton1.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if (ajRadioButton1.isSelected()) {
						nextclass = "selectkeygui";
					}

				}
			});
			arbgroup.add(ajRadioButton1);
		}
		return ajRadioButton1;
	}
	
	private JRadioButton getbJRadioButton1() {
		if (bjRadioButton1 == null) {
			bjRadioButton1 = new JRadioButton();
			bjRadioButton1.setBounds(new Rectangle(137, 130, 165, 21));
			bjRadioButton1.setText((String) labels.get("jRadioButton1"));
			bjRadioButton1.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if (bjRadioButton1.isSelected()) {
						nextclass = "selectkeygui";
					}

				}
			});
			brbgroup.add(bjRadioButton1);
		}
		return bjRadioButton1;
	}

	/**
	 * This method initializes jRadioButton2
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getaJRadioButton2() {
		if (ajRadioButton2 == null) {
			ajRadioButton2 = new JRadioButton();
			ajRadioButton2.setBounds(new Rectangle(100, 175, 120, 25));
			ajRadioButton2.setText("PKCS1Padding");
			ajRadioButton2.setSelected(true);
			setPadding("PKCS1Padding");
			ajRadioButton2.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if (ajRadioButton2.isSelected()) {
						setPadding("PKCS1Padding");
					}

				}
			});
			arb2group.add(ajRadioButton2);
		}
		return ajRadioButton2;
	}
	
	private JRadioButton getbJRadioButton2() {
		if (bjRadioButton2 == null) {
			bjRadioButton2 = new JRadioButton();
			bjRadioButton2.setBounds(new Rectangle(100, 175, 120, 25));
			bjRadioButton2.setText("PKCS1Padding");
			bjRadioButton2.setSelected(true);
			setPadding("PKCS1Padding");
			bjRadioButton2.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if (bjRadioButton2.isSelected()) {
						setPadding("PKCS1Padding");
					}

				}
			});
			brb2group.add(bjRadioButton2);
		}
		return bjRadioButton2;
	}

	/**
	 * This method initializes jRadioButton3
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getaJRadioButton3() {
		if (ajRadioButton3 == null) {
			ajRadioButton3 = new JRadioButton();
			ajRadioButton3.setBounds(new Rectangle(220, 175, 120, 25));
			ajRadioButton3.setText("PKCS7Padding");
			ajRadioButton3.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if (ajRadioButton3.isSelected()) {
						setPadding("PKCS7Padding");
					}

				}
			});
			arb2group.add(ajRadioButton3);
		}
		return ajRadioButton3;
	}
	
	private JRadioButton getbJRadioButton3() {
		if (bjRadioButton3 == null) {
			bjRadioButton3 = new JRadioButton();
			bjRadioButton3.setBounds(new Rectangle(220, 175, 120, 25));
			bjRadioButton3.setText("PKCS7Padding");
			bjRadioButton3.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if (bjRadioButton3.isSelected()) {
						setPadding("PKCS7Padding");
					}

				}
			});
			brb2group.add(bjRadioButton3);
		}
		return bjRadioButton3;
	}

	public void setDataFile(String file) {
		this.dataFile = file;
		if (!file.equals(null)) {
			if(selected == 0) {
				ajLabel4.setText((String) labels.get("jLabel4") + ": " + file);
			} else if(selected == 1) {
				bjLabel4.setText((String) labels.get("jLabel4") + ": " + file);
			}
			// jLabel10.setVisible(true);
		}
	}

	public void setKeyAndCert(Certificate cert, PrivateKey privateKey) {
		this.cert = cert;
		this.privateKey = privateKey;
		if (!cert.equals("null") || !privateKey.equals("null")) {
			// jLabel11.setVisible(true);
		}
	}
	
	public void setCert(Certificate cert) {
		this.publicKeyCert = cert;
		if (!publicKeyCert.equals("null")) {
			// jLabel11.setVisible(true);
		}
	}

	public void setPadding(String padding) {
		this.padding = "RSA/ECB/" + padding;
	}

	public AsymmetricRSAEncryption passclass() {
		return this;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
